/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JOrderTypeEditor.java
 *
 * Created on 2009-11-10, 19:49:25
 */

package cn.ekuma.epos.order;

import cn.ekuma.data.dao.bean.I_Category;
import cn.ekuma.data.ui.swing.AbstractParentDAOJEditor;
import cn.ekuma.data.ui.swing.JChildDAOEditorPanel;
import cn.ekuma.epos.bean.util.erp.OrderTypeHelper;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.I_DataLogicSystem;
import cn.ekuma.epos.datalogic.businesslogic.OrderProcessManager;
import cn.ekuma.epos.datalogic.businesslogic.orderprocess.I_OrderProcessFlow;
import cn.ekuma.epos.datalogic.businesslogic.orderprocess.I_ProcessManager;
import cn.ekuma.epos.order.model.OrderProcessConfigTableModel;
import cn.ekuma.epos.order.model.OrderRuleConfigTableModel;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValEntry;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.pos.admin.ResourceType;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.bean.AttributeSet;
import com.openbravo.bean.erp.OrderType;
import com.openbravo.bean.erp.OrderTypeCategory;
import com.openbravo.pos.panels.JCategoryTreeDialog;

import java.awt.Component;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.swing.DefaultComboBoxModel;

import org.ofsm.impl.FileFiniteStateMachineModel;
import org.ofsm.creator.StateDiagramManager;

import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;

/**
 *
 * @author Administrator
 */
@SuppressWarnings("serial")
public class JOrderTypeEditor extends AbstractParentDAOJEditor<OrderType,String>{

    private ComboBoxValModel attmodel,ableOrderProcessModel,selectedOrderProcessModel;
    private ComboBoxValModel m_CategoryModel;
    private DefaultComboBoxModel m_StateMachineModel;
    private FileFiniteStateMachineModel model ;
    private StateDiagramManager stateDiagramMgr;
    private I_DataLogicSystem dlSystem;
    private I_DataLogicERP dlSales;
   private  JChildDAOEditorPanel orderTypeProcessEditor;
    private JChildDAOEditorPanel orderTypeRuleEditor;
	private JCheckBox chkRule;
    
    
    /** Creates new form JOrderTypeEditor */
    public JOrderTypeEditor(I_DataLogicERP dlSales,I_DataLogicSystem dlSystem,DirtyManager dirty,I_ProcessManager processManager) {
        super(dirty);
        initComponents();
        this.dlSystem=dlSystem;
        this.dlSales=dlSales;
        jPriceType.addItem(new ComboBoxValEntry(OrderType.PRICETYPE_BuyPrice,AppLocal.getIntString("list.OrderType.PRICETYPE_BuyPrice")));
        jPriceType.addItem(new ComboBoxValEntry(OrderType.PRICETYPE_SellPrice,AppLocal.getIntString("list.OrderType.PRICETYPE_SellPrice")));
        jPriceType.addItem(new ComboBoxValEntry(OrderType.PRICETYPE_CustomerPrice,AppLocal.getIntString("list.OrderType.PRICETYPE_CustomerPrice")));
        jPriceType.addItem(new ComboBoxValEntry(OrderType.PRICETYPE_CustomerCategoryPrice,AppLocal.getIntString("list.OrderType.PRICETYPE_CustomerCategoryPrice")));
        jPriceType.addItem(new ComboBoxValEntry(OrderType.PRICETYPE_WholesalePrice,AppLocal.getIntString("list.OrderType.PRICETYPE_WholesalePrice")));
        jPriceType.addItem(new ComboBoxValEntry(OrderType.PRICETYPE_WholesaleCustomerCategoryPrice,AppLocal.getIntString("list.OrderType.PRICETYPE_WholesaleCustomerCategoryPrice")));

        jStoreType.addItem(new ComboBoxValEntry(OrderType.STORE_In,AppLocal.getIntString("list.OrderType.STORE_In")));
        jStoreType.addItem(new ComboBoxValEntry(OrderType.STORE_Out,AppLocal.getIntString("list.OrderType.STORE_Out")));
        jStoreType.addItem(new ComboBoxValEntry(OrderType.STORE_NoChange,AppLocal.getIntString("list.OrderType.STORE_NoChange")));
        jStoreType.addItem(new ComboBoxValEntry(OrderType.STORE_BalanceIn,AppLocal.getIntString("list.OrderType.STORE_BalanceIn")));
        jStoreType.addItem(new ComboBoxValEntry(OrderType.STORE_BalanceOut,AppLocal.getIntString("list.OrderType.STORE_BalanceOut")));

        jStoreType1.addItem(new ComboBoxValEntry(OrderType.STORE_In,AppLocal.getIntString("list.OrderType.STORE_In")));
        jStoreType1.addItem(new ComboBoxValEntry(OrderType.STORE_Out,AppLocal.getIntString("list.OrderType.STORE_Out")));
        jStoreType1.addItem(new ComboBoxValEntry(OrderType.STORE_NoChange,AppLocal.getIntString("list.OrderType.STORE_NoChange")));
        jStoreType1.addItem(new ComboBoxValEntry(OrderType.STORE_BalanceIn,AppLocal.getIntString("list.OrderType.STORE_BalanceIn")));
        jStoreType1.addItem(new ComboBoxValEntry(OrderType.STORE_BalanceOut,AppLocal.getIntString("list.OrderType.STORE_BalanceOut")));

        jConsultType.addItem(new ComboBoxValEntry(OrderType.CONSULTTYPE_Null,AppLocal.getIntString("list.OrderType.CONSULTTYPE_Null")));
        jConsultType.addItem(new ComboBoxValEntry(OrderType.CONSULTTYPE_Order,AppLocal.getIntString("list.OrderType.CONSULTTYPE_Order")));
        jConsultType.addItem(new ComboBoxValEntry(OrderType.CONSULTTYPE_TimeSliceStore,AppLocal.getIntString("list.OrderType.CONSULTTYPE_TimeSliceStore")));
        jConsultType.addItem(new ComboBoxValEntry(OrderType.CONSULTTYPE_RealStore,AppLocal.getIntString("list.OrderType.CONSULTTYPE_RealStore")));

        jFundType.addItem(new ComboBoxValEntry(OrderType.FUNDTYPE_NoChange,AppLocal.getIntString("list.OrderType.FUNDTYPE_NoChange")));
        jFundType.addItem(new ComboBoxValEntry(OrderType.FUNDTYPE_In,AppLocal.getIntString("list.OrderType.FUNDTYPE_In")));
        jFundType.addItem(new ComboBoxValEntry(OrderType.FUNDTYPE_Out,AppLocal.getIntString("list.OrderType.FUNDTYPE_Out")));

        jOrderLineType.addItem(new ComboBoxValEntry(OrderType.ORDERLINETYPE_SupportAll,AppLocal.getIntString("list.OrderType.ORDERLINETYPE_SupportAll")));
        jOrderLineType.addItem(new ComboBoxValEntry(OrderType.ORDERLINETYPE_NotSupportAll,AppLocal.getIntString("list.OrderType.ORDERLINETYPE_NotSupportAll")));
        jOrderLineType.addItem(new ComboBoxValEntry(OrderType.ORDERLINETYPE_SupportProduct,AppLocal.getIntString("list.OrderType.ORDERLINETYPE_SupportProduct")));
        jOrderLineType.addItem(new ComboBoxValEntry(OrderType.ORDERLINETYPE_SupportUniqueProduct,AppLocal.getIntString("list.OrderType.ORDERLINETYPE_SupportUniqueProduct")));
        jOrderLineType.addItem(new ComboBoxValEntry(OrderType.ORDERLINETYPE_SupportUniqueProductAdded,AppLocal.getIntString("list.OrderType.ORDERLINETYPE_SupportUniqueProductAdded")));
        jOrderLineType.addItem(new ComboBoxValEntry(OrderType.ORDERLINETYPE_SupportService,AppLocal.getIntString("list.OrderType.ORDERLINETYPE_SupportService")));
        jOrderLineType.addItem(new ComboBoxValEntry(OrderType.ORDERLINETYPE_SupportProductAndService,AppLocal.getIntString("list.OrderType.ORDERLINETYPE_SupportProductAndService")));
        jOrderLineType.addItem(new ComboBoxValEntry(OrderType.ORDERLINETYPE_SupportUniqueProductAndService,AppLocal.getIntString("list.OrderType.ORDERLINETYPE_SupportUniqueProductAndService")));
        jOrderLineType.addItem(new ComboBoxValEntry(OrderType.ORDERLINETYPE_SupportUniqueProductAddAndService,AppLocal.getIntString("list.OrderType.ORDERLINETYPE_SupportUniqueProductAddAndService")));

        jConsultPriceType.addItem(new ComboBoxValEntry(OrderType.PRICETYPE_noConsult,AppLocal.getIntString("list.OrderType.CONSULTTYPE_Null")));
        jConsultPriceType.addItem(new ComboBoxValEntry(OrderType.PRICETYPE_BuyPrice,AppLocal.getIntString("list.OrderType.PRICETYPE_BuyPrice")));
        jConsultPriceType.addItem(new ComboBoxValEntry(OrderType.PRICETYPE_SellPrice,AppLocal.getIntString("list.OrderType.PRICETYPE_SellPrice")));
        jConsultPriceType.addItem(new ComboBoxValEntry(OrderType.PRICETYPE_CustomerPrice,AppLocal.getIntString("list.OrderType.PRICETYPE_CustomerPrice")));
        jConsultPriceType.addItem(new ComboBoxValEntry(OrderType.PRICETYPE_CustomerCategoryPrice,AppLocal.getIntString("list.OrderType.PRICETYPE_CustomerCategoryPrice")));
        jConsultPriceType.addItem(new ComboBoxValEntry(OrderType.PRICETYPE_WholesalePrice,AppLocal.getIntString("list.OrderType.PRICETYPE_WholesalePrice")));
        jConsultPriceType.addItem(new ComboBoxValEntry(OrderType.PRICETYPE_WholesaleCustomerCategoryPrice,AppLocal.getIntString("list.OrderType.PRICETYPE_WholesaleCustomerCategoryPrice")));
        
        
        jOrderName.getDocument().addDocumentListener(dirty);
        jOrderReverseName.getDocument().addDocumentListener(dirty);
        jSubjectRef.getDocument().addDocumentListener(dirty);
        jPriceType.addActionListener(dirty);
        jStoreType.addActionListener(dirty);
        jConsultType.addActionListener(dirty);
        jFundType.addActionListener(dirty);
        jOrderLineType.addActionListener(dirty);
        jCashAddAble.addActionListener(dirty);
        jChangePrice.addActionListener(dirty);
        m_jAtt.addActionListener(dirty);
        jStoreType1.addActionListener(dirty);
       m_jCategory.addActionListener(dirty);
       jStateMachine.addActionListener(dirty);
       jProductConsult.addActionListener(dirty);
       jServiceCheck.addActionListener(dirty);
       jOrderProcessComb.addActionListener(dirty);
       
       jConsultPriceType.addActionListener(dirty);
       chkRule.addActionListener(dirty);
       
       
       // The attributes model
        attmodel = new ComboBoxValModel();
        m_CategoryModel = new ComboBoxValModel();

       model = new FileFiniteStateMachineModel();
       model.setSafeUnmarshallingMode(true);
       stateDiagramMgr = new StateDiagramManager(model, diagramGraph);
       
       ableOrderProcessModel=new ComboBoxValModel();
       selectedOrderProcessModel=new ComboBoxValModel();
       

       Map<String,I_OrderProcessFlow> processMap=processManager.getProcesFlowMap();
       for(I_OrderProcessFlow process:processMap.values()){
          jOrderProcessComb.addItem(process.getName());
        }

       orderTypeProcessEditor=new  JChildDAOEditorPanel(dlSales,new OrderProcessConfigTableModel(),new JOrderTypeProcessEditor(new DirtyManager(),dlSales, processManager));
       jTabbedPane1.add(orderTypeProcessEditor.getTabName(), orderTypeProcessEditor);
       addObjectChangeListener(orderTypeProcessEditor); 
       
       orderTypeRuleEditor=new  JChildDAOEditorPanel(dlSales,new OrderRuleConfigTableModel(),new JOrderTypeRuleEditor(new DirtyManager(),dlSales));
       jTabbedPane1.add(orderTypeRuleEditor.getTabName(), orderTypeRuleEditor);
       addObjectChangeListener(orderTypeRuleEditor); 
    
    }
    
    public void activate() throws BasicException {
        attmodel = new ComboBoxValModel(dlSales.list(AttributeSet.class));
        attmodel.add(0, null);
        m_jAtt.setModel(attmodel);

        List a;
        try {
            a = dlSales.list(OrderTypeCategory.class);
        } catch (BasicException eD) {
            MessageInf msg = new MessageInf(MessageInf.SGN_NOTICE, AppLocal.getIntString("message.cannotloadlists"), eD);
            msg.show(this);
            a = new ArrayList();
        }

        a.add(0, null); // The null item
        m_CategoryModel = new ComboBoxValModel(a);
        m_jCategory.setModel(m_CategoryModel);

        try {
            a = dlSystem.listResourceNameByType(ResourceType.STATE.getM_iKey());
        } catch (BasicException eD) {
            MessageInf msg = new MessageInf(MessageInf.SGN_NOTICE, AppLocal.getIntString("message.cannotloadlists"), eD);
            msg.show(this);
            a = new ArrayList();
        }
        if(a==null)
            a = new ArrayList();
        m_StateMachineModel=new DefaultComboBoxModel(a.toArray());
        jStateMachine.setModel(m_StateMachineModel);
        updateVisualization();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
    	java.awt.GridBagConstraints gridBagConstraints;
    	
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jOrderName = new javax.swing.JTextField();
        jOrderReverseName = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPriceType = new javax.swing.JComboBox();
        jConsultType = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jStoreType = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jSubjectRef = new javax.swing.JTextField();
        jChangePrice = new javax.swing.JCheckBox();
        jLabel7 = new javax.swing.JLabel();
        jFundType = new javax.swing.JComboBox();
        m_jAtt = new javax.swing.JComboBox();
        jLabel13 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jOrderLineType = new javax.swing.JComboBox();
        jCashAddAble = new javax.swing.JCheckBox();
        jLabel9 = new javax.swing.JLabel();
        m_jCategory = new javax.swing.JComboBox();
        jSearchCategory = new javax.swing.JButton();
        jStoreType1 = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jStateMachine = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();
        jProductConsult = new javax.swing.JCheckBox();
        jLabel14 = new javax.swing.JLabel();
        jOrderProcessComb = new javax.swing.JComboBox();
        jServiceCheck = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jAddState = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        diagramGraph = new org.jgraph.JGraph();

        setLayout(new java.awt.BorderLayout());

        jLabel3.setText(AppLocal.getIntString("label.ordertype.name")); // NOI18N

        jLabel5.setText(AppLocal.getIntString("label.ordertype.pricttype")); // NOI18N

        jLabel4.setText(AppLocal.getIntString("label.ordertype.reverseName")); // NOI18N

        jLabel6.setText(AppLocal.getIntString("label.ordertype.consulttype")); // NOI18N

        jLabel1.setText(AppLocal.getIntString("label.ordertype.storetype")); // NOI18N

        jLabel2.setText(AppLocal.getIntString("label.ordertype.subjectRef")); // NOI18N

        jChangePrice.setText(AppLocal.getIntString("label.ordertype.changePrice")); // NOI18N

        jLabel7.setText(AppLocal.getIntString("label.ordertype.fundtype")); // NOI18N

        jLabel13.setText(AppLocal.getIntString("label.attributes")); // NOI18N

        jLabel8.setText(AppLocal.getIntString("label.ordertype.orderlinetype")); // NOI18N

        jOrderLineType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jOrderLineTypeActionPerformed(evt);
            }
        });

        jCashAddAble.setText(AppLocal.getIntString("label.ordertype.cashaddable")); // NOI18N

        jLabel9.setText(AppLocal.getIntString("label.prodcategory")); // NOI18N

        jSearchCategory.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/search.png"))); // NOI18N
        jSearchCategory.setFocusable(false);
        jSearchCategory.setPreferredSize(new java.awt.Dimension(35, 25));
        jSearchCategory.setRequestFocusEnabled(false);
        jSearchCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSearchCategoryActionPerformed(evt);
            }
        });

        jLabel10.setText(AppLocal.getIntString("label.ordertype.storetype1")); // NOI18N

        jLabel11.setText(AppLocal.getIntString("label.StateMachine")); // NOI18N

        jLabel12.setText(AppLocal.getIntString("label.productConsult")); // NOI18N

        jLabel14.setText(AppLocal.getIntString("label.ordertype.orderprocess")); // NOI18N

        jServiceCheck.setText(AppLocal.getIntString("label.ordertype.servicecheck")); // NOI18N
        
        lblNewLabel = new JLabel(AppLocal.getIntString("label.ordertype.consultPriceType"));
        
        jConsultPriceType = new JComboBox();
        
        JLabel label = new JLabel(AppLocal.getIntString("label.ordertype.checkRule"));
        
        chkRule = new JCheckBox("");

        javax.swing.GroupLayout gl_jPanel1 = new javax.swing.GroupLayout(jPanel1);
        gl_jPanel1.setHorizontalGroup(
        	gl_jPanel1.createParallelGroup(Alignment.TRAILING)
        		.addGroup(Alignment.LEADING, gl_jPanel1.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.LEADING)
        				.addGroup(gl_jPanel1.createSequentialGroup()
        					.addGroup(gl_jPanel1.createParallelGroup(Alignment.TRAILING)
        						.addComponent(lblNewLabel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
        						.addComponent(jLabel14, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
        						.addComponent(jLabel13, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
        						.addComponent(jLabel7, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
        						.addComponent(jLabel2, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 269, Short.MAX_VALUE)
        						.addComponent(jLabel8, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
        						.addComponent(jLabel11, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
        						.addComponent(jLabel9, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
        						.addComponent(jLabel12, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
        						.addComponent(jLabel1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
        						.addComponent(jLabel4, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 269, Short.MAX_VALUE)
        						.addComponent(jLabel6, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
        						.addComponent(jLabel5, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
        						.addComponent(jLabel3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
        						.addComponent(jLabel10, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE))
        					.addPreferredGap(ComponentPlacement.UNRELATED))
        				.addGroup(gl_jPanel1.createSequentialGroup()
        					.addComponent(label, GroupLayout.PREFERRED_SIZE, 243, GroupLayout.PREFERRED_SIZE)
        					.addGap(36)))
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.LEADING)
        				.addComponent(chkRule)
        				.addGroup(gl_jPanel1.createParallelGroup(Alignment.LEADING, false)
        					.addGroup(gl_jPanel1.createSequentialGroup()
        						.addGroup(gl_jPanel1.createParallelGroup(Alignment.LEADING, false)
        							.addComponent(jOrderName, GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
        							.addComponent(jOrderReverseName, GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
        							.addComponent(jPriceType, 0, 186, Short.MAX_VALUE)
        							.addComponent(jConsultType, 0, 186, Short.MAX_VALUE)
        							.addComponent(jStoreType, 0, 186, Short.MAX_VALUE)
        							.addComponent(jStoreType1, 0, 186, Short.MAX_VALUE)
        							.addComponent(jSubjectRef, GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
        							.addComponent(jOrderLineType, 0, 186, Short.MAX_VALUE)
        							.addComponent(jFundType, GroupLayout.PREFERRED_SIZE, 186, GroupLayout.PREFERRED_SIZE)
        							.addComponent(m_jAtt, GroupLayout.PREFERRED_SIZE, 186, GroupLayout.PREFERRED_SIZE))
        						.addGroup(gl_jPanel1.createParallelGroup(Alignment.LEADING)
        							.addGroup(gl_jPanel1.createSequentialGroup()
        								.addPreferredGap(ComponentPlacement.UNRELATED)
        								.addComponent(jServiceCheck, GroupLayout.PREFERRED_SIZE, 159, GroupLayout.PREFERRED_SIZE))
        							.addGroup(gl_jPanel1.createSequentialGroup()
        								.addGap(2)
        								.addGroup(gl_jPanel1.createParallelGroup(Alignment.LEADING)
        									.addComponent(jChangePrice, GroupLayout.PREFERRED_SIZE, 179, GroupLayout.PREFERRED_SIZE)
        									.addComponent(jCashAddAble, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE))))
        						.addGap(16))
        					.addComponent(jStateMachine, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        					.addGroup(gl_jPanel1.createSequentialGroup()
        						.addComponent(m_jCategory, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
        						.addPreferredGap(ComponentPlacement.RELATED)
        						.addComponent(jSearchCategory, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
        					.addComponent(jProductConsult)
        					.addComponent(jOrderProcessComb, 0, 186, Short.MAX_VALUE)
        					.addComponent(jConsultPriceType, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE)))
        			.addContainerGap())
        );
        gl_jPanel1.setVerticalGroup(
        	gl_jPanel1.createParallelGroup(Alignment.LEADING)
        		.addGroup(gl_jPanel1.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.LEADING, false)
        				.addGroup(gl_jPanel1.createSequentialGroup()
        					.addGap(6)
        					.addComponent(jLabel3))
        				.addComponent(jOrderName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.UNRELATED)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.LEADING, false)
        				.addGroup(gl_jPanel1.createSequentialGroup()
        					.addGap(6)
        					.addComponent(jLabel4))
        				.addComponent(jOrderReverseName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.LEADING, false)
        				.addGroup(gl_jPanel1.createSequentialGroup()
        					.addGap(13)
        					.addComponent(jLabel5))
        				.addGroup(gl_jPanel1.createSequentialGroup()
        					.addGap(7)
        					.addGroup(gl_jPanel1.createParallelGroup(Alignment.BASELINE)
        						.addComponent(jPriceType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        						.addComponent(jChangePrice))))
        			.addPreferredGap(ComponentPlacement.UNRELATED)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.LEADING, false)
        				.addGroup(gl_jPanel1.createSequentialGroup()
        					.addGap(6)
        					.addComponent(jLabel6))
        				.addComponent(jConsultType, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
        			.addGap(12)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.BASELINE, false)
        				.addComponent(jLabel1)
        				.addComponent(jStoreType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel10)
        				.addComponent(jStoreType1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.BASELINE, false)
        				.addComponent(jLabel2)
        				.addComponent(jSubjectRef, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
        				.addComponent(jCashAddAble))
        			.addPreferredGap(ComponentPlacement.UNRELATED)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel7)
        				.addComponent(jFundType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.UNRELATED)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel13)
        				.addComponent(m_jAtt, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel8)
        				.addComponent(jOrderLineType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(jServiceCheck))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.LEADING)
        				.addGroup(gl_jPanel1.createParallelGroup(Alignment.BASELINE)
        					.addComponent(jLabel9)
        					.addComponent(m_jCategory, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
        				.addComponent(jSearchCategory, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel11)
        				.addComponent(jStateMachine, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.LEADING)
        				.addComponent(jLabel12)
        				.addComponent(jProductConsult))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel14)
        				.addComponent(jOrderProcessComb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblNewLabel)
        				.addComponent(jConsultPriceType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.LEADING)
        				.addComponent(label)
        				.addComponent(chkRule))
        			.addContainerGap(62, Short.MAX_VALUE))
        );
        gl_jPanel1.linkSize(SwingConstants.HORIZONTAL, new Component[] {jPriceType, jConsultType, jStoreType, jSubjectRef, jFundType, m_jAtt, jOrderLineType, m_jCategory, jStoreType1, jStateMachine, jOrderProcessComb, jOrderName, jOrderReverseName});
        jPanel1.setLayout(gl_jPanel1);

        jTabbedPane1.addTab(AppLocal.getIntString("Label.baseDefine"), jPanel1); // NOI18N

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel4.setLayout(new java.awt.BorderLayout());

        jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.LINE_AXIS));

        jAddState.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/reload.png"))); // NOI18N
        jAddState.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jAddStateActionPerformed(evt);
            }
        });
        jPanel3.add(jAddState);

        jPanel4.add(jPanel3, java.awt.BorderLayout.NORTH);

        jPanel5.setLayout(new java.awt.BorderLayout());
        jPanel4.add(jPanel5, java.awt.BorderLayout.CENTER);
        jPanel4.add(diagramGraph, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel4, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab(AppLocal.getIntString("label.StateMachine"), jPanel2); // NOI18N

        add(jTabbedPane1, java.awt.BorderLayout.CENTER);
        
    }// </editor-fold>//GEN-END:initComponents

	private void jSearchCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSearchCategoryActionPerformed
        JCategoryTreeDialog dailog=JCategoryTreeDialog.newJDialog(this);
        dailog.showDialog(m_CategoryModel.getDatas(),(I_Category) m_CategoryModel.getSelectedItem());
        if(dailog.isOK())
            m_CategoryModel.setSelectedItem(dailog.getSelected());
}//GEN-LAST:event_jSearchCategoryActionPerformed

    private void jAddStateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAddStateActionPerformed
        updateVisualization();
    }//GEN-LAST:event_jAddStateActionPerformed

    private void jOrderLineTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jOrderLineTypeActionPerformed
        int selectLineType=(Integer)((ComboBoxValEntry)jOrderLineType.getSelectedItem()).getKey();
        if(OrderTypeHelper.isServiceSuport(selectLineType))
            jServiceCheck.setEnabled(true);
        else{
            jServiceCheck.setEnabled(false);
            jServiceCheck.setSelected(false);
        }
    }//GEN-LAST:event_jOrderLineTypeActionPerformed

    private void updateVisualization() {
        String stateMachineName=(String) jStateMachine.getSelectedItem();
        if(stateMachineName!=null)
        {
            byte[] bf=dlSystem.getResourceAsBinary(stateMachineName);
            if(bf!=null){
                ByteArrayInputStream  btStream=new ByteArrayInputStream(bf);
                try {
                    model.unmarshal(btStream);
                } catch (Exception ex) {
                    MessageInf msg = new MessageInf(MessageInf.SGN_NOTICE, AppLocal.getIntString("message.notStateMachineDOC"), ex);
                    msg.show(this);
                }
            }
        }
        stateDiagramMgr.setupModel();
        diagramGraph.updateUI();
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jgraph.JGraph diagramGraph;
    private javax.swing.JButton jAddState;
    private javax.swing.JCheckBox jCashAddAble;
    private javax.swing.JCheckBox jChangePrice;
    private javax.swing.JComboBox jConsultType;
    private javax.swing.JComboBox jFundType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JComboBox jOrderLineType;
    private javax.swing.JTextField jOrderName;
    private javax.swing.JComboBox jOrderProcessComb;
    private javax.swing.JTextField jOrderReverseName;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JComboBox jPriceType;
    private javax.swing.JCheckBox jProductConsult;
    private javax.swing.JButton jSearchCategory;
    private javax.swing.JCheckBox jServiceCheck;
    private javax.swing.JComboBox jStateMachine;
    private javax.swing.JComboBox jStoreType;
    private javax.swing.JComboBox jStoreType1;
    private javax.swing.JTextField jSubjectRef;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JComboBox m_jAtt;
    private javax.swing.JComboBox m_jCategory;
    private JLabel lblNewLabel;
    private JComboBox jConsultPriceType;
    // End of variables declaration//GEN-END:variables

    @Override
    public void refresh() {  
    }

    @Override
    public Component getComponent() {
        return this;
    }

    @Override
    public OrderType createNew(Properties prop) {
        return new OrderType();
    }

    @Override
    public void rebuild(OrderType obj) throws BasicException {
        if(m_StateMachineModel.getSelectedItem()==null)
            throw new BasicException("Please Select a State Machine for this Order Type!");
        obj.setName(jOrderName.getText());
        obj.setReverseName(jOrderReverseName.getText());
        obj.setInOutType((Integer)((ComboBoxValEntry)jStoreType.getSelectedItem()).getKey());
        obj.setPriceType((Integer)((ComboBoxValEntry)jPriceType.getSelectedItem()).getKey());
        obj.setChangePrice(jChangePrice.isSelected());
        obj.setConsultType((Integer)((ComboBoxValEntry)jConsultType.getSelectedItem()).getKey());
        obj.setSubjectRefId(jSubjectRef.getText());
        obj.setFundType((Integer)((ComboBoxValEntry)jFundType.getSelectedItem()).getKey());
        obj.setAttributesetid((String)attmodel.getSelectedKey());
        obj.setOrderLineType((Integer)((ComboBoxValEntry)jOrderLineType.getSelectedItem()).getKey());
        obj.setCashAddAble(jCashAddAble.isSelected());
        obj.setCategoryid((String)m_CategoryModel.getSelectedKey());
        obj.setInOutType1((Integer)((ComboBoxValEntry)jStoreType1.getSelectedItem()).getKey());
        obj.setStateMachine((String)m_StateMachineModel.getSelectedItem());
        obj.setProductConsult(jProductConsult.isSelected());
        obj.setProcessName((String) jOrderProcessComb.getSelectedItem());
        obj.setServiceCheck(jServiceCheck.isSelected());
        obj.setConsultPriceType((Integer)((ComboBoxValEntry)jConsultPriceType.getSelectedItem()).getKey());
        obj.setCheckRule(chkRule.isSelected());
        List<ComboBoxValEntry> entrys=selectedOrderProcessModel.getDatas();
        List<String> selectedOrderProcess=new ArrayList();
        for(ComboBoxValEntry entry:entrys){
        	selectedOrderProcess.add((String) entry.getKey());
        }
        obj.setSelectedOrderProcess(org.apache.commons.lang.StringUtils.join(selectedOrderProcess.iterator(), ","));
    }

    @Override
    public void updateUI(boolean editAble, int state) {
        jOrderName.setEnabled(editAble);
        jOrderReverseName.setEnabled(editAble);
        jSubjectRef.setEnabled(editAble);
        jPriceType.setEnabled(editAble);
        jStoreType.setEnabled(editAble);
        jStoreType1.setEnabled(editAble);
        jConsultType.setEnabled(editAble);
        jFundType.setEnabled(editAble);
         m_jAtt.setEnabled(editAble);
        jOrderLineType.setEnabled(editAble);
        jCashAddAble.setEnabled(editAble);
        jStateMachine.setEnabled(editAble);
        m_jCategory.setEnabled(editAble);
        jSearchCategory.setEnabled(editAble);

        jConsultPriceType.setEnabled(editAble);
        chkRule.setEnabled(editAble);
        
        if(state==this.STATE_Insert){
          jChangePrice.setSelected(false);
          jProductConsult.setSelected(false);
        }else{
            jChangePrice.setSelected(editAble);
            jProductConsult.setSelected(editAble);
        }

        jOrderName.setText(obj.getName());
        jOrderReverseName.setText(obj.getReverseName());
        jStoreType.setSelectedItem(ComboBoxValEntry.buildEntryByKey(obj.getInOutType()));
        jPriceType.setSelectedItem(ComboBoxValEntry.buildEntryByKey(obj.getPriceType()));
        jChangePrice.setSelected(obj.isChangePrice());
        jConsultType.setSelectedItem(ComboBoxValEntry.buildEntryByKey(obj.getConsultType()));
        jSubjectRef.setText(obj.getSubjectRefId());
        jFundType.setSelectedItem(ComboBoxValEntry.buildEntryByKey(obj.getFundType()));
        attmodel.setSelectedKey(obj.getAttributesetid());
        jOrderLineType.setSelectedItem(ComboBoxValEntry.buildEntryByKey(obj.getOrderLineType()));
        jCashAddAble.setSelected(obj.isCashAddAble());
        m_CategoryModel.setSelectedKey(obj.getCategoryid());
        jStoreType1.setSelectedItem(ComboBoxValEntry.buildEntryByKey(obj.getInOutType1()));
        m_StateMachineModel.setSelectedItem(obj.getStateMachine());
        jProductConsult.setSelected(obj.isProductConsult());
        jOrderProcessComb.setSelectedItem(obj.getProcessName());
        jServiceCheck.setSelected(obj.isServiceCheck());
        jConsultPriceType.setSelectedItem(ComboBoxValEntry.buildEntryByKey(obj.getConsultPriceType()));
        chkRule.setSelected(obj.isCheckRule());
        
         if(editAble&&OrderTypeHelper.isServiceSuport(obj.getOrderLineType()))
            jServiceCheck.setEnabled(editAble);
        else
            jServiceCheck.setEnabled(false);
        updateVisualization();
        
        ableOrderProcessModel.refresh(new ArrayList());
        selectedOrderProcessModel.refresh(new ArrayList());
    }

    @Override
    public Class getEditClass() {
        return OrderType.class;
    }
}
